package com.health.httpbean.upload;

import android.util.Log;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.blankj.utilcode.util.UiMessageUtils;
import com.google.gson.Gson;
import com.health.httpbean.HttpConstant;
import com.health.httpbean.HttpDataBean;
import com.health.httpbean.HttpResultBean;
import com.health.httpbean.QueryTypeEnum;
import com.health.httpbean.format.FormatGxbData;
import com.health.tablebean.BD_DD_INFO;
import com.health.tablebean.webservice.CoronaryHeartDisease;
import com.health.viewscontrols.NovaApplication;
import com.health.viewscontrols.settings.DataDownLoadMainActivity;

import java.util.List;

import rxhttp.RxHttp;

/**
 * author : xiaoxin
 * date   : 2020/12/19 18:54
 * desc   :
 * update :
 */
public class GxbUpload {

    private static final String TAG = "GxbUpload";

    private DataDownLoadMainActivity activity;
    private int gxbFTotalSize = 0;
    private int gxbFSuccessSize = 0;
    private int gxbFErrorSize = 0;

    public void upLoadGxb(DataDownLoadMainActivity activity) {
        this.activity = activity;
        Gson gson = new Gson();
        List<CoronaryHeartDisease> tnbList = NovaApplication.fdbmanger.findAllByWhere(CoronaryHeartDisease.class,
                "IS_UPDATE = 'Y'");
        if (tnbList.size() <= 0) {
            ToastUtils.showShort("无数据可上传");
            UiMessageUtils.getInstance().send(7000,"冠心病无随访数据可上传");
        }else {
            gxbFTotalSize = tnbList.size();
            UiMessageUtils.getInstance().send(7001,"冠心病数据" + tnbList.size() + "条正在上传，请稍候");
            List<BD_DD_INFO> listZZs = NovaApplication.fdbmanger.findAllByWhereInInfoDB(BD_DD_INFO.class,
                    "PARENTID='DM04-06'");
            for (int i = 0; i < tnbList.size(); i++) {
                HttpDataBean httpDataBean = new HttpDataBean();
                httpDataBean.QueryType = QueryTypeEnum.GXB.getCode();
                httpDataBean.Data = gson.toJson(FormatGxbData.formatGxb(tnbList.get(i),listZZs));
                upLoadHttpGxb(GsonUtils.toJson(httpDataBean),tnbList.get(i),i);
            }
        }
    }

    private void upLoadHttpGxb(String json, CoronaryHeartDisease model, final int position) {
        RxHttp.postJson(HttpConstant.getHttpUrlUpLoadData())
                .addAll(json)
                .asClass(HttpResultBean.class)
                .subscribe(resultBean -> {
                    LogUtils.e(resultBean);
                    if (resultBean.getResponseCode() == 200) {
                        gxbFSuccessSize++;
                        model.setIS_UPDATE("N");
                        upDataGxb(model);
                    } else {
                        gxbFErrorSize++;
                        ToastUtils.showShort("" + resultBean.getResponseMsg());
                    }
                    if (gxbFTotalSize-1 == position) {
                        Log.e(TAG, "upLoadHttpGxb: 7000" );
                        UiMessageUtils.getInstance().send(7000,
                                "冠心病随访数据上传成功" + gxbFSuccessSize+"条" + " 失败" + gxbFErrorSize + "条");
                    }
                }, throwable -> {
                    gxbFErrorSize++;
                    LogUtils.e(throwable.getMessage());
                });
    }

    public static void upDataGxb(CoronaryHeartDisease model) {
        NovaApplication.fdbmanger.updata(model, "cardid='" + model.getCardid() + "'");
    }
}
